☁️ cloud | June 29, 2021
AWS 인프라 자원을 생성할 [YAML 파일]
Parameters:
KeyName:
Description: Name of an existing EC2 KeyPair to enable SSH access to the instances. Linked to AWS Parameter
Type: AWS::EC2::KeyPair::KeyName
ConstraintDescription: must be the name of an existing EC2 KeyPair.
LatestAmiId:
Description: (DO NOT CHANGE)
Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
Default: '/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2'
AllowedValues:
- /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2
Resources:
MyVPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.0.0.0/16
EnableDnsSupport: true
EnableDnsHostnames: true
Tags:
- Key: Name
Value: My-VPC
MyIGW:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value: My-IGW
MyIGWAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
InternetGatewayId: !Ref MyIGW
VpcId: !Ref MyVPC
MyPublicRT:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref MyVPC
Tags:
- Key: Name
Value: My-Public-RT
MyDefaultPublicRoute:
Type: AWS::EC2::Route
DependsOn: MyIGWAttachment
Properties:
RouteTableId: !Ref MyPublicRT
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref MyIGW
MyPublicSN1:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref MyVPC
AvailabilityZone: !Select [0, !GetAZs '']
CidrBlock: 10.0.0.0/24
Tags:
- Key: Name
Value: My-Public-SN-1
MyPublicSN2:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref MyVPC
AvailabilityZone: !Select [2, !GetAZs '']
CidrBlock: 10.0.1.0/24
Tags:
- Key: Name
Value: My-Public-SN-2
MyPublicSNRouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref MyPublicRT
SubnetId: !Ref MyPublicSN1
MyPublicSNRouteTableAssociation2:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref MyPublicRT
SubnetId: !Ref MyPublicSN2
WEBSG:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable HTTP access via port 80 and SSH access via port 22
VpcId: !Ref MyVPC
Tags:
- Key: Name
Value: WEBSG
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '80'
ToPort: '80'
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp: 0.0.0.0/0
MYEC21:
Type: AWS::EC2::Instance
Properties:
InstanceType: t2.micro
ImageId: !Ref LatestAmiId
KeyName: !Ref KeyName
Tags:
- Key: Name
Value: EC2-1
NetworkInterfaces:
- DeviceIndex: 0
SubnetId: !Ref MyPublicSN1
GroupSet:
- !Ref WEBSG
AssociatePublicIpAddress: true
UserData:
Fn::Base64: !Sub |
#!/bin/bash
hostname EC2-1
yum install httpd -y
service httpd start
chkconfig httpd on
echo "<h1>CloudNet@ EC2-1 Web Server</h1>" > /var/www/html/index.html
MYEC22:
Type: AWS::EC2::Instance
Properties:
InstanceType: t2.micro
ImageId: !Ref LatestAmiId
KeyName: !Ref KeyName
Tags:
- Key: Name
Value: EC2-2
NetworkInterfaces:
- DeviceIndex: 0
SubnetId: !Ref MyPublicSN2
GroupSet:
- !Ref WEBSG
AssociatePublicIpAddress: true
UserData:
Fn::Base64: !Sub |
#!/bin/bash
hostname ELB-EC2-2
yum install httpd -y
service httpd start
chkconfig httpd on
echo "<h1>CloudNet@ EC2-2 Web Server</h1>" > /var/www/html/index.html
MyEIP1:
Type: AWS::EC2::EIP
Properties:
Domain: vpc
MyEIP1Assoc:
Type: AWS::EC2::EIPAssociation
Properties:
InstanceId: !Ref MYEC21
AllocationId: !GetAtt MyEIP1.AllocationId
MyEIP2:
Type: AWS::EC2::EIP
Properties:
Domain: vpc
MyEIP2Assoc:
Type: AWS::EC2::EIPAssociation
Properties:
InstanceId: !Ref MYEC22
AllocationId: !GetAtt MyEIP2.AllocationId
ALBTargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
Name: My-ALB-TG
Port: 80
Protocol: HTTP
VpcId: !Ref MyVPC
Targets:
- Id: !Ref MYEC21
Port: 80
- Id: !Ref MYEC22
Port: 80
ApplicationLoadBalancer:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
Name: My-ALB
Scheme: internet-facing
SecurityGroups:
- !Ref WEBSG
Subnets:
- !Ref MyPublicSN1
- !Ref MyPublicSN2
ALBListener:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
DefaultActions:
- Type: forward
TargetGroupArn: !Ref ALBTargetGroup
LoadBalancerArn: !Ref ApplicationLoadBalancer
Port: 80
Protocol: HTTP
사용자 PC에서 ALB DNS 주소로 웹 접근하여 로드밸런싱 되는 기본 환경을 검증
단순 레코드 정의
2대의 EC2 인스턴스를 연결하여 주 대상(Primary) 보조 대상(Secondary) 형태의 장애 조치 라우팅을 구성합니다.
주 대상(Primary-Check), 보조 대상(Secondary-Check) 상태 생성
/index
경로를 주기적으로 상태 검사함장애 조치 라우팅 생성
레코드 정의 클릭
주 대상 장애를 일으키기 위해 EC2-1 인스턴스 중지 후 테스트
상태 검사 확인
상태 검사 주기에 따라 인스턴스의 상태를 확인하고 파악하는데 약간의 딜레이가 발생할 수 있습니다. 원하는 동작이 이루어지지 않았을 때 잠시 후 다시 시도해봐야 합니다.